package example.documentbased.documentModule.mediators
{
	import example.documentbased.documentModule.controller.IDocumentController;
	import example.documentbased.documentModule.views.IDocumentModuleViewContract;
	import example.documentbased.editorModule.controller.IEditorController;
	import example.documentbased.editorModule.models.EditorModuleModel;
	import example.documentbased.mainModule.models.vo.DocumentVO;
	
	import flash.utils.setTimeout;
	
	import mx.collections.ArrayList;
	
	import org.etherframework.common.IDestroyable;
	import org.etherframework.controller.Responder;
	import org.etherframework.mediator.IActivatable;
	import org.etherframework.mediator.Mediator;
	
	public class DocumentModuleViewMediator extends Mediator implements 
		IDocumentModuleViewContract, IDestroyable, IActivatable
	{
		private var _editorModel:EditorModuleModel;
		private var _editedDocuments:ArrayList;
		private var _editorController:IEditorController;
		private var _documentController:IDocumentController;
		private var _documentVO:DocumentVO;
		private var _color:uint;
		private var _text:String;
		private var _documentName:String;
		private var _textChanged:Boolean;
		private var _isBusy:Boolean = false;
		
		public function DocumentModuleViewMediator(editorModel:EditorModuleModel, 
												   editorController:IEditorController, 
												   documentController:IDocumentController) 
		{
			_editorModel = editorModel;
			_editorController = editorController;
			_documentController = documentController;
		}

		override public function complete():void
		{
			_editedDocuments = _editorModel.editedDocuments;
			bindView("data", dataChangeHandler);
		}
		
		public function textChanged(value:String):void
		{	
			_documentVO.text = value;
			
			if (!_textChanged)
			{
				_textChanged = true;
				setTimeout(commitText, 300);
			}
		}
		
		private function commitText():void
		{
			_editorController.getEditDocumentCommand(_documentVO).execute();
			_textChanged = false;
		}
		
		public function activate():void
		{
			if (_documentVO && _documentVO.lazy && !_documentVO.text &&
				!_editorModel.getIsEditedDocument(_documentVO)) 
			{
				isBusy = true;
				_documentController.getGetDocumentContentCommand(_documentVO)
					.respond(new Responder(contentResultHandler, contentFaultHandler));
			}
		}
		
		public function deactivate():void
		{
			_documentController.cancelAll();
		}
		
		public function destroy():void
		{
			trace("Document \"" + _documentName + "\" has been destroyed.");
		}
		
		private function dataChangeHandler(value:DocumentVO):void
		{
			_documentVO = value;
			
			if (_documentVO)
			{
				// keeping name just for this example to have nice 
				// printing while document destruction.
				_documentName = _documentVO.name;
				
				if (_documentVO.lazy)
				{
					activate();
				}
				else
				{
					text = _documentVO.text;
				}
			}
		}
		
		private function contentResultHandler(value:String):void
		{
			_documentVO.text = value;
			text = value;
			_editorController.getUpdateDocumentCommand(_documentVO).execute();
			isBusy = false;
		}
		
		private function contentFaultHandler(value:*):void
		{
			isBusy = false;
			// do something?
		}
		
		[Bindable]
		public function get text():String { return _text; }
		public function set text(value:String):void { _text = value; }

		[Bindable]
		public function get color():uint { return _color; }
		public function set color(value:uint):void { _color = value; }
		
		[Bindable]
		public function get isBusy():Boolean { return _isBusy; }
		public function set isBusy(value:Boolean):void { _isBusy = value; }
	}
}